/**
 * 
 */
package cn.com.checne.controller.superManager;

import java.awt.Color;
import java.awt.Font;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;

import javax.imageio.ImageIO;

import org.apache.commons.io.FilenameUtils;

import cn.com.checne.dao.WatermarkDao;
import cn.com.checne.model.domain.Watermark;
import cn.com.checne.util.watermark.ImageTool;

/**
 * @author zy
 *
 */
public class WatermarkManager {
	
	private WatermarkDao wmDao = new WatermarkDao() ;

	//水印位置
	//String toward[] = { "右下", "左下", "右上", "左上", "正中", "左上->右下","左下->右下" };
	//字体样式
	//String fontstyle[] = {"普通","粗体","斜体","粗体&斜体"};
	public final int fontstyles[] = {Font.PLAIN, Font.BOLD, Font.ITALIC, Font.BOLD | Font.ITALIC};
	
	//字体颜色
	//String fontcolor[] = {"蓝色","青色","绿色","红色","灰色","深灰色","浅灰色","洋红色","桔黄色","粉红色","黄色","黑色","白色"};
	public final Color colors[] = {Color.blue, Color.cyan, Color.green, Color.red, Color.gray,Color.darkGray, Color.lightGray, 
			Color.magenta, Color.orange, Color.pink,Color.yellow, Color.black, Color.white};
	
	//缩放比例
	//String scales[] = {"不缩放", "25%", "50%", "75%","125%","150%","175%","200%"};
	public final float imgScales[] = {1.0f, 0.25f, 0.5f, 0.75f, 1.25f, 1.5f, 1.75f, 2.0f};
	//透明度
	//float alpha [] = {0.0f,0.1f,0.2f,0.3f,0.4f,0.5f,0.6f,0.7f,0.8f,0.9f,1.0f};
	
	
	public String toWater(int lg,String srcImage){
		String destImage = null ;
		Watermark wm = wmDao.getWatermarkByLgId(lg);
		if(wm == null){
			return null ;
		}
		
		Color fontColor = this.colors[wm.getFontColor()];
		//水印位置
		int toward = wm.getToward();
		//添加水印的字体
		Font f = new Font(wm.getFont(), this.fontstyles[wm.getFontStyle()], (int)(wm.getFontSize() * this.imgScales[wm.getScale()]));
		//放缩比例
		float scale = this.imgScales[wm.getScale()];
		
		try {
			BufferedImage buffImg = ImageTool.watermark(srcImage, f, fontColor,toward, wm.getMark(), wm.getAlpha(), scale);
			destImage = srcImage.substring(0,srcImage.lastIndexOf(".")) + "_wm." + FilenameUtils.getExtension(srcImage) ;
			ImageIO.write(buffImg, FilenameUtils.getExtension(srcImage), new File(destImage));
		} catch (IOException e) {
			e.printStackTrace();
		}
		return destImage ;
	}
	
	public boolean isWaterOpen(int languageId){
		return wmDao.isWaterOpen(languageId);
	}
	
}
